import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

class Test_gwc():
    def setup_method(self):
        options = webdriver.ChromeOptions()
        options.add_experimental_option('detach', True)
        self.driver = webdriver.Chrome(options=options)
        # self.driver = webdriver.Chrome()
        self.driver.get("http://127.0.0.1/index.php/Home/user/login.html")
        self.driver.maximize_window()
        self.driver.find_element(By.ID, 'username').send_keys('18325539630')
        self.driver.find_element(By.ID, 'password').send_keys('wsf123456')
        self.driver.find_element(By.ID, 'verify_code').send_keys('8888')
        self.driver.find_element(By.NAME, 'sbtbutton').click()

        WebDriverWait(self.driver, 5, 0.5).until(lambda
        x: x.find_element(By.LINK_TEXT, '返回商城首页')).click()

        js = "window.scrollTo(0, 1000)"
        self.driver.execute_script(js)

    def test01_gwc(self):
        name01 = self.driver.find_element(By.CSS_SELECTOR,  'a[href="/Home/Goods/goodsInfo/id/65.html"] div[class="googs-title ellipsis-1"]').text
        price01=self.driver.find_element(By.CSS_SELECTOR, 'a[href="/Home/Goods/goodsInfo/id/65.html"] div[class="googs-price ellipsis-1"]').text

        element = self.driver.find_element(By.CSS_SELECTOR, "div[class='goods-pic'] img[src='/public/upload/goods/thumb/65/goods_thumb_65_400_400.jpeg']")
        # 创建鼠标对象
        action = ActionChains(self.driver)
        # 调用鼠标双击事件方法
        action.double_click(element)
        # 调用鼠标执行方法
        action.perform()

        self.driver.find_elements(By.ID, 'join_cart')[0].click()

        name02 = self.driver.find_element(By.CSS_SELECTOR,                                                          'div[class="detail-ggsl"] h1').text
        price02 = self.driver.find_element(By.CSS_SELECTOR,
                    'li  span[id="goods_price"]').text

        WebDriverWait(self.driver, 5, 0.5).until(lambda
        x: x.find_element(By.CLASS_NAME, 'layui-layer-ico')).click()

        # 进入我的购物车
        self.driver.find_element(By.CSS_SELECTOR, 'div[class="c-n fl"] span').click()
        self.driver.find_element(By.CSS_SELECTOR,
        'td[align="center"] a[class="gwc-gb deleteGoods"]').click()

        assert  name01[0:19] in  name02
        assert  price01 ==price02

    def test02_gwc(self):
        aa = 'a[href="/Home/Goods/goodsInfo/id/65.html"] div[class="googs-title ellipsis-1"]'
        name01 = self.driver.find_element(By.CSS_SELECTOR, aa)

        ele = "div[class='goods-pic'] img[src='/public/upload/goods/thumb/65/goods_thumb_65_400_400.jpeg']"
        element = self.driver.find_element(By.CSS_SELECTOR, ele)

        # 创建鼠标对象
        action = ActionChains(self.driver)
        # 调用鼠标双击事件方法
        action.double_click(element)
        # 调用鼠标执行方法
        action.perform()

        self.driver.find_element(By.CLASS_NAME, 'add').click()

        # 点击加入购物车

        self.driver.find_elements(By.ID, 'join_cart')[0].click()

        # name2=driver.find_element(By.CSS_SELECTOR,'div[class="detail-ggsl"] h1').text

        WebDriverWait(self.driver, 5, 0.5).until(lambda
        x: x.find_element(By.CLASS_NAME, 'layui-layer-ico')).click()

        number = self.driver.find_element(By.ID, 'cart_quantity').text
        print(number)
        # 进入我的购物车
        self.driver.find_element(By.CSS_SELECTOR, 'div[class="c-n fl"] span').click()
        # 获取商品金额
        # 清楚购物车
        self.driver.find_element(By.CSS_SELECTOR,
        'td[align="center"] a[class="gwc-gb deleteGoods"]').click()


